/****************************************************
 * @class BomReportGridPanel
 * @extends Ext.Window
 ***************************************************/
	BomReportGridPanel = Ext.extend(Ext.grid.GridPanel,{
		condition: null,
		store: null,
		cm: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.condition = {};
			this.condition.limit = 100;
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition,   
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/report/bomReport.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'trackingNumber'
				},[
					{name: 'material_FK'},
					{name: 'prodVersion_FK'},
					{name: 'fab'},
					{name: 'orderTask'},
					{name: 'bomMemo'},
					{name: 'trackingNumber'}
				])
			});
			
			this.cm = new Ext.grid.ColumnModel([
		        {
		           header: "成品料号",
		           dataIndex: 'material_FK',
		           sortable: true,
		           width: 100
		        },{
		           header: "版本",
		           dataIndex: 'prodVersion_FK',
		           width: 50
		        },{
		           header: "适用厂别",
		           dataIndex: 'fab',
		           width: 50
		        },{
		           header: "表单状态",
		           dataIndex: 'orderTask',
		           width: 80
		        },{
		           header: "申请原因",
		           dataIndex: 'bomMemo',
		           width: 150
		        },{
		           header: "BOM单号",
		           dataIndex: 'trackingNumber',
		           sortable: true,
		           width: 80,
		           renderer: renderHref
		        }]);
		        	        
			BomReportGridPanel.superclass.constructor.call(this, {
				enableColumnMove: false,
				enableHdMenu: false,
				//enableColumnHide: false,
			    loadMask: {msg:"信息载入中..."},
			    viewConfig: {forceFit: true},
				region: 'center',
				border: true,
				autoHeight: true,
				layout: 'fit',
				split: true,
				stripeRows: true,
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
				cm: this.cm,
				trackMouseOver: false,
				bbar: new Ext.PagingToolbar({
					pageSize: 100,
					store: this.store,
					displayInfo: true
				})
			});
		},
		load: function(c){
			this.condition.trackingNumber = c;
			this.store.load();
		}
	});
		
	function renderHref(v){
		v = "<a href=\"javascript:openBR('"+v+"')\" >"+v+"</a>";
		return v;
	}
	
	function openBR(v){
		jsonrpc = new JSONRpcClient("/pdm/JSON-RPC");
		try{
			var info = jsonrpc.jabsorbService.getDispatchUrl(v, Ext.fly("login_user").dom.value,"PP.BR");
		}catch(e){
			alert(e);
		}
		
		window.open("http://myivo.ivo.com.cn/Arcadia/servlet/WebDispatcher?mailCode="+info);
		
	}

/****************************************************
 * @class SelectedMaterialGrid
 * @extends Ext.Window
 ***************************************************/
	SelectedMaterialGrid = Ext.extend(Ext.grid.GridPanel,{
		condition: null,
		store: null,
		cm: null,
		btn_auth: null,
		win_MaterialProduVer: null,
		hidden_BoMParam: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.condition = {};
			this.condition.limit = 200;
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition,   
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/report/bomReport.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'trackingNumber'
				},[
					{name: 'material_FK'},
					{name: 'prodVersion_FK'},
					{name: 'fab'},
					{name: 'orderTask'},
					{name: 'bomMemo'},
					{name: 'trackingNumber'}
				])
			});
			
			this.cm = new Ext.grid.ColumnModel([
		        {
		           header: "成品料号",
		           dataIndex: 'material_FK',
		           sortable: true,
		           width: 80
		        },{
		           header: "版本",
		           dataIndex: 'prodVersion_FK',
		           width: 50
		        },{
		           header: "适用厂别",
		           dataIndex: 'fab',
		           width: 50
		        }]);
	        
			SelectedMaterialGrid.superclass.constructor.call(this, {
				enableColumnMove: false,
				enableHdMenu: false,
			    loadMask: {msg: "信息载入中..."},
			    viewConfig: {forceFit: true},
				region: 'center',
				border: true,
				autoHeight: true,
				layout: 'fit',
				split: true,
				stripeRows: true,
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
				cm: this.cm,
				trackMouseOver: false,
				tbar: ['->',{
					text: "添加",
					scope: this,
					iconCls: 'add',
					tooltip: "选择条件",
					hidden: (this.btn_auth=="false"),
					handler: function(){
						if(Ext.isEmpty(this.win_MaterialProduVer))
						{
							var aa = new MaterialProduVerGrid();
							this.win_MaterialProduVer = new MaterialProduVerWindow({materialProduVerGrid: aa, hidden_BoMParam: this.hidden_BoMParam});
						}
						this.win_MaterialProduVer.show();
					}
				},'-',{
					text: "删除",
					scope: this,
					iconCls: 'delete',
					hidden: (this.btn_auth=="false"),
					handler: function(){
						var record = this.getSelectionModel().getSelected();
						if(Ext.isEmpty(record))
						{
							Ext.Msg.alert("提示", "请单击选中需要删除的行！");
						}else{
							var trackingNumber = record.get("trackingNumber");
							var bomParam = Ext.fly(this.hidden_BoMParam).dom.value;
							
							if(bomParam.indexOf(trackingNumber)==0)
							{
								bomParam = bomParam.replace(trackingNumber+',', '');
								bomParam = bomParam.replace(trackingNumber, '');
							}
							else if(bomParam.indexOf(trackingNumber)>0)
							{
								bomParam = bomParam.replace(','+trackingNumber, '');
							}
							else{
								Ext.Msg.alert("提示","糟糕，好像系统出问题了!");
							}
							
							Ext.fly(this.hidden_BoMParam).dom.value = bomParam;
							var gp_BomReport = Ext.getCmp("gp_BomReport");
							
							this.load(bomParam);
							gp_BomReport.load(bomParam);
							
						}
					}
				}]
			});
		},
		load: function(c){
			this.condition.trackingNumber = c;
			this.store.load();
		}
	});

/****************************************************
 * @class MaterialProduVerWindow
 * @extends Ext.Window
 ***************************************************/
	MaterialProduVerWindow = Ext.extend(Ext.Window,{
		materialProduVerGrid: null,
		hidden_BoMParam: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			MaterialProduVerWindow.superclass.constructor.call(this, {
				layout: 'fit',
				width: 420,
				height: 480,
				modal: true,
				plain: true,
				resizable: false,
				closeAction: 'hide',
				buttonAlign: 'center',
				items: this.materialProduVerGrid,
				buttons: [{
					text: "确定",
					scope: this,
					handler: function(){
						var record = this.materialProduVerGrid.getSelectionModel().getSelected();
						if(Ext.isEmpty(record))
						{
							Ext.Msg.alert("提示","请选择相应的成品料号！");
						}else{
							var trackingNumber = record.get("trackingNumber");
							if(Ext.fly(this.hidden_BoMParam).dom.value.indexOf(trackingNumber)!=-1)
							{
								Ext.Msg.alert("提示","成品料号 ["+record.get("material_FK")+"] 已选择！");
							}else{
								if(Ext.fly(this.hidden_BoMParam).dom.value != "")
								{
									trackingNumber = Ext.fly(this.hidden_BoMParam).dom.value+","+trackingNumber;
								}
								Ext.fly(this.hidden_BoMParam).dom.value = trackingNumber;
								
								var gp_SelectedMaterial = Ext.getCmp("gp_SelectedMaterial");
								var gp_BomReport = Ext.getCmp("gp_BomReport");
								
								this.hide();
								
								gp_SelectedMaterial.load(trackingNumber);
								gp_BomReport.load(trackingNumber);
								
							}
						}
					}
				},{
					text: "关闭",
					scope: this,
					handler: function(){
						this.hide();
					}
				}]
			});
		}
	});
	
/****************************************************
 * @class MaterialProduVerGrid
 * @extends Ext.Window
 ***************************************************/
	MaterialProduVerGrid = Ext.extend(Ext.grid.GridPanel,{
		condition: null,
		store: null,
		cm: null,
		searchField: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.condition = {};
			this.condition.limit = 200;
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition,   
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/report/queryBomReport.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'trackingNumber'
				},[
					{name: 'material_FK'},
					{name: 'prodVersion_FK'},
					{name: 'fab'},
					{name: 'orderTask'},
					{name: 'bomMemo'},
					{name: 'trackingNumber'}
				])
			});
			
			this.cm = new Ext.grid.ColumnModel([
		        {
		           header: "成品料号",
		           dataIndex: 'material_FK',
		           sortable: true,
		           width: 80
		        },{
		           header: "版本",
		           dataIndex: 'prodVersion_FK',
		           width: 50
		        },{
		           header: "适用厂别",
		           dataIndex: 'fab',
		           width: 50
		        }]);
		    
		    this.store.load();
	        
	        this.searchField = new Ext.ux.form.SearchField({store: this.store, width: 200});
	        
			MaterialProduVerGrid.superclass.constructor.call(this, {
				enableColumnMove: false,
			    loadMask: {msg: "信息载入中..."},
			    viewConfig: {forceFit: true},
				region: 'center',
				border: false,
				height: 500,
				layout: 'fit',
				split: true,
				stripeRows: true,
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
				cm: this.cm,
				trackMouseOver: false,
				tbar: ["查询：",this.searchField],
				bbar: new Ext.PagingToolbar({
					pageSize: 200,
					store: this.store,
					displayInfo: true
				})
			});
		}
	});
